package com.example.afinal;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;

public class ListActivity extends AppCompatActivity {
//查看数据库中存储的所有人对mbti的看法理解，方便做关于此类的研究项目数据获取以及个人兴趣了解
    private ListView mListView;

    private DBHelper mDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        mDbHelper = new DBHelper(this);

        mListView = findViewById(R.id.list_mb);

        ArrayList<HashMap<String, String>> userList = new ArrayList<>();

        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        String[] projection = {
                DBHelper.COLUMN_ID,
                DBHelper.COLUMN_MBTI,
                DBHelper.COLUMN_COMMENT
        };

        Cursor cursor = db.query(
                DBHelper.TABLE,
                projection,
                null,
                null,
                null,
                null,
                null
        );

        while (cursor.moveToNext()) {
            HashMap<String, String> user = new HashMap<>();
            user.put("id", cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_ID)));
            user.put("mbti", cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_MBTI)));
            user.put("comment", cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_COMMENT)));
            userList.add(user);
        }

        cursor.close();

        SimpleAdapter adapter = new SimpleAdapter(
                this,
                userList,
                android.R.layout.simple_list_item_2,
                new String[]{"mbti", "comment"},
                new int[]{android.R.id.text1, android.R.id.text2});
        mListView.setAdapter(adapter);
    }
}